﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RelationExpertSystem.Models;
using System.Windows.Forms;

namespace RelationExpertSystem.Controllers
{
    public class PeopleController
    {
        private EditPeopleDialog _editor;
        private PeopleModel _model;

        public event EventHandler<FactsEventArgs> FactsLoaded
        {
            add { _model.FactsLoaded += value; }
            remove { _model.FactsLoaded -= value; }
        }

        public event EventHandler<FactsEventArgs> FactsUpdated
        {
            add { _model.FactsUpdated += value; }
            remove { _model.FactsUpdated -= value; }
        }

        public PeopleController()
        {
            _model = new PeopleModel(null);
            _editor = new EditPeopleDialog(this);
        }

        public void LoadPeople()
        {
            _model.LoadPeople();
        }

        public void EditPeople()
        {
            _editor.LoadPeople(_model.GetPeople());
            _editor.ShowDialog(); // modal
        }

        public void NewPeople()
        {
            _model.NewPeople();
            _editor.ShowDialog();
        }

        public void UpdatePeople(List<Person> people)
        {
            _model.UpdatePeople(people);
        }

        public void SavePeople()
        {
            _model.SavePeople();
        }

        public void SavePeople(List<Person> people)
        {
            UpdatePeople(people);
            SavePeople();
        }

        public IEnumerable<Person> GetPeople()
        {
            return _model.GetPeople();
        }

        public void ClearInferredRelations()
        {
            _model.ClearInferredRelations();
        }

        public void Close()
        {
            _editor.DialogResult = DialogResult.OK;
            _editor.Close();
        }

        public IEnumerable<IRuleTarget<RelationEventArgs>> GetPairs()
        {
            return _model.GetPairs();
        }
    }
}
